#include"../include/lib_headers.h"

char* copy_file(char *source_file_with_path, char *destination_path) {
	node *temp1, *temp2;
	
	/* VFS not mounted */
	if (h == NULL || l1 == NULL) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_05);
		return output_message;
	}

	/* Insufficient Arguments */
	if (strlen(source_file_with_path) == 0 || strlen(destination_path) == 0) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_00);
		return output_message;
	}

	/* Both the arguments are the same */ 
/*	if (strcmp(source_file_with_path, destination_path) == 0) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_02);
		return output_message;
	}
*/	/* IF SOURCE ITSELF IS THE ROOT */
	if(strcmp(source_file_with_path,"/")==0){
		sprintf(output_message, "%s %s", "copyfile_FAILURE",ERR_VFS_COPYFILE_03);
		return output_message;
	} 
	/* IF NO DOTS ARE THERE THEN IT'S CLEAR THAT SOURCE FILE PATH DOES NOT EXIST */
	if ( strchr ( source_file_with_path , '.' ) == NULL ){
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_01);
		return output_message;
	} 

	/* CHECKING IF DESTINATION HAS A FILE PATH */
	if ( strchr ( destination_path , '.' ) == NULL ){
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_02);
		return output_message;
	}
	
	
	/* EXTENSION OF BOTH SOURCE FILE PATH AND DESTINATION FILE PATH MUST BE SAME */
	if( strcmp ( strrchr ( source_file_with_path , '.' ) + 1 , strrchr ( destination_path , '.' ) + 1 ) == 0 ) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",
				ERR_VFS_COPYFILE_02);
		return output_message;
	} 

	/* File System is full */
	if (h->usedFds == h->maxFds) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",ERR_VFS_COPYFILE_04);
		return output_message;
	}
	
	/* Source file cannot be found */
	if (find_node(l1->next, source_file_with_path) == NULL || source_file_with_path[0] != '/' ) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",ERR_VFS_COPYFILE_01);
		return output_message;
	} 
	else
		temp1 = find_node(l1->next, source_file_with_path);
	
	/* The destination Path does not exist i.e the directory does not exist*/
	if(find_node(l1->next, getparent(destination_path)) == NULL || destination_path[0] != '/' ){
		sprintf(output_message, "%s %s", "copyfile_FAILURE",ERR_VFS_COPYFILE_02);
		return output_message;
	}	
	
	/* Source file path turned out to be a directory */
	if (strcmp(find_node(l1->next, source_file_with_path)->fd_pointer->fileType, "dir") == 0) {
		sprintf(output_message, "%s %s", "copyfile_FAILURE",ERR_VFS_COPYFILE_03);
		return output_message;
	} 

	/* Calling a function to add the file at the destination */
	addfile_from_vfs(destination_path, temp1);
	sprintf(output_message, "%s", "copyfile_SUCCESS");
	return output_message;
	
}

